package com.ptmall.app.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.ptmall.app.R;
import com.ptmall.app.bean.HomePageTuijianBean;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.CateListActivity;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.NoScrollGridView;
import com.ptmall.app.view.NoScrollGridViewDirver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by zjh on 2016/9/23.
 * 新品推荐
 */
public class NewGoodsAdapter extends RecyclerView.Adapter<NewGoodsAdapter.ViewHolder> {

    GoodListAdapter3 mGoodListAdapter;
    private Activity mActivity;
    private List<HomePageTuijianBean> mData = new ArrayList();

    public NewGoodsAdapter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public void bind(List<HomePageTuijianBean> mData) {
        this.mData = mData;
    }

    public interface Click{
        void addcarimg(ImageView iv,String id,String num);

    }

    public Click click;

    public void setClick(Click click) {
        this.click = click;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_homepagetuijianlist, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        HomePageTuijianBean bean = mData.get(position);
//        GlideUtil.showImg(mActivity,bean.head_image,viewHolder.tuijianback);
        Glide.with(mActivity).load(HttpMethods.BASEIMG_URL+bean.head_image).dontAnimate().placeholder(R.drawable.z1).error(R.drawable.z1).into(viewHolder.tuijianback);

        viewHolder.tuijianback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, CateListActivity.class);
                intent.putExtra("id",bean.styleId);
                mActivity.startActivity(intent);
            }
        });
        viewHolder.title.setText(bean.style_name);
        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, CateListActivity.class);
                intent.putExtra("id",bean.styleId);
                mActivity.startActivity(intent);
            }
        });
        mGoodListAdapter = new GoodListAdapter3(mActivity,bean.productlist);
        viewHolder.freshgridview.setAdapter(mGoodListAdapter);
        viewHolder.freshgridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mActivity, GoodDetilActivity.class);
//                intent.putExtra("id",bean.productlist.get(i).productId);
                mActivity.startActivity(intent);
            }
        });
        mGoodListAdapter.setClickInterface(new GoodListAdapter3.ClickInterface() {
            @Override
            public void shopcaradd(ImageView iv, String id, int position) {
                click.addcarimg(iv,id,"1");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView title,more;
        ImageView img,tuijianback;
        NoScrollGridViewDirver freshgridview;

        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            more = (TextView) view.findViewById(R.id.more);
            freshgridview = (NoScrollGridViewDirver) view.findViewById(R.id.freshgridview);
            tuijianback = (ImageView) view.findViewById(R.id.tuijianback);
        }
    }



    private void addcar(String id,String num) {
        Map<String, String> map = new HashMap<>();
        map.put("productId", id);
        map.put("num", num);
        ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                Toast.makeText(mActivity,"加入购物车成功",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
